//
//  HomeViewController.swift
//  SinaWeiBo
//
//  Created by tuyue on 16/3/5.
//  Copyright © 2016年 xueyang.co.ltd. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
private let HomeCellId = "HomeCellId"

class HomeViewController: BaseTableViewController {
    
    lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !userLogin {
            visitorLoginView?.setupInfoWith(nil, lbText: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        prepareForTableview()
        
        Status.loadHomeData { (array) -> () in
            guard let list = array else {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            
            self.statuses = list
            self.tableView.reloadData()
        }
        
    }
    
    private func prepareForTableview(){
        //    tableView.registerClass(UITableViewCell.self, forHeaderFooterViewReuseIdentifier: HomeCellId)
        //    }
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
    }
}


extension HomeViewController{
    
    // MARK: - Table view data source
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        
        let s = statuses[indexPath.row]
        
        cell.status = s
        print(s.imageURLs?.count)
        
        //        cell.textLabel?.text = statuses[indexPath.row].user?.name
        
        return cell
    }
    
}
